package com.jingyanzi.member.controller;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jingyanzi.member.api.SessionClientAPI;
import com.jingyanzi.member.domain.LoginDO;
import com.jingyanzi.member.domain.SaleContactRecordDO;
import com.jingyanzi.member.domain.result.ResultDO;
import com.jingyanzi.member.domain.result.ResultSupport;
import com.jingyanzi.member.query.SaleContactRecordQueryDO;
import com.jingyanzi.member.response.PageResponse;
import com.jingyanzi.member.service.ISalesRecordService;
import com.jingyanzi.member.util.MemberConstant;
import com.jingyanzi.resallocation.domain.SingleRequest;
import com.jingyanzi.resallocation.result.SingleResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "业务记录查询controller", tags = { "业务记录查询接口" })
@RestController
@RequestMapping("/record")
public class SaleContactRecordController {

	public static final String SYS_ERROR = "{status:500, msg:\"系统错误\", version:1}";

	public static final String DES = "3DES";

	public static final String PLANINTEXT = "plaintext";

	@Autowired
	private ISalesRecordService salesRecordService;

	@Autowired
	SessionClientAPI sessionClientAPI;

	private LoginDO getLoginDO(String token) {
		Base64.Encoder encoder = Base64.getEncoder();
		LoginDO loginDO = new LoginDO();
		String t;
		try {
			t = encoder.encodeToString(token.getBytes("utf-8"));
			loginDO = sessionClientAPI.getSession(t);
			if (loginDO == null) {
				loginDO = new LoginDO();
				loginDO.setId(1);
				loginDO.setMobile("13800000000");
				loginDO.setNickname("罗于红");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return loginDO;
	}

	@ApiOperation(value = "创建客户活动记录", notes = "创建客户活动记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "业务记录对象", required = true, dataType = "SingleRequest<SaleContactRecordDO>") })
	@RequestMapping(value = "/add.json", method = RequestMethod.POST)
	public SingleResponse<SaleContactRecordDO> add(@RequestBody SingleRequest<SaleContactRecordDO> request) {

		SaleContactRecordDO domain = request.getData();

		LoginDO loginDO = getLoginDO(request.getToken());
		domain.setAuthor(loginDO.getNickname());
		domain.setAuthorId(loginDO.getId());
		domain.setEmployeeId(loginDO.getId());
		domain.setEmployeeName(loginDO.getNickname());
		domain.setIsAddAuto(MemberConstant.FLAG_FALSE); // 手动添加

		ResultDO result = salesRecordService.add(domain);
		SingleResponse<SaleContactRecordDO> response = new SingleResponse<SaleContactRecordDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("添加成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("添加失败");
		}
		return response;
	}

	@ApiOperation(value = "修改客户活动信息", notes = "修改客户业务记录信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户业务记录对象", required = true, dataType = "SingleRequest<SaleContactRecordDO>") })
	@RequestMapping(value = "/edit.json", method = RequestMethod.POST)
	public SingleResponse<SaleContactRecordDO> edit(@RequestBody SingleRequest<SaleContactRecordDO> request) {

		SaleContactRecordDO domain = request.getData();

		ResultDO result = salesRecordService.add(domain);
		domain.setModifi(getLoginDO(request.getToken()).getNickname());
		domain.setModifiId(getLoginDO(request.getToken()).getId());

		SingleResponse<SaleContactRecordDO> response = new SingleResponse<SaleContactRecordDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("修改成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("修改失败");
		}
		return response;
	}

	@ApiOperation(value = "获取客户业务记录详情", notes = "获取客户业务记录详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户业务记录对象(只传客户的ID)", required = true, dataType = "SingleRequest<CustomerFamilyDO>") })
	@RequestMapping(value = "/detail.json", method = RequestMethod.POST)
	public SingleResponse<SaleContactRecordDO> detail(@RequestBody SingleRequest<SaleContactRecordDO> request) {

		SaleContactRecordDO domain = request.getData();

		ResultDO result = new ResultSupport();
		// customerService.add(domain);
		SingleResponse<SaleContactRecordDO> response = new SingleResponse<SaleContactRecordDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("获取成功");
			response.setData(domain);
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("获取失败");
		}
		return response;
	}

	@ApiOperation(value = "修改客户业务记录扩展信息", notes = "修客户业务信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户业务记录对象(只传客户的ID)", required = true, dataType = "SingleRequest<CustomerFamilyDO>") })
	@RequestMapping(value = "/delete.json", method = RequestMethod.POST)
	public SingleResponse<SaleContactRecordDO> delete(@RequestBody SingleRequest<SaleContactRecordDO> request) {

		// SaleContactRecordDO domain = request.getData();

		ResultDO result = new ResultSupport();
		// customerService.add(domain);
		SingleResponse<SaleContactRecordDO> response = new SingleResponse<SaleContactRecordDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("删除成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("删除失败");
		}
		return response;
	}

	@ApiOperation(value = "查询客户业务记录信息", notes = "查询客户业务记录信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "查询查询对象", required = true, dataType = "SingleRequest<SaleContactRecordQueryDO>") })
	@RequestMapping(value = "/query.json", method = RequestMethod.POST)
	public PageResponse<SaleContactRecordDO> query(@RequestBody SingleRequest<SaleContactRecordQueryDO> request) {

		SaleContactRecordQueryDO query = request.getData();

		if (query.getPageRows() > 0) {
			query.setPage(true);
		}
		ResultDO result = salesRecordService.query(query);

		PageResponse<SaleContactRecordDO> response = new PageResponse<SaleContactRecordDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("查询成功");
			response.setData((List<SaleContactRecordDO>) result.getModel(ResultDO.FIRST_MODEL_KEY));
			if (query.isPage()) {
				response.setTotal((Long) result.getModel(ResultDO.SECOND_MODEL_KEY));
			}
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("查询失败");
		}
		return response;
	}
}
